﻿using log4net;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Telerik.Windows.Controls;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMGoodsView.xaml
  /// </summary>
  public partial class DMGoodsView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMCustomerView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public ObservableCollection<GoodsModel> GoodsList { get; set; }
    public GoodsModel SelectedGoods { get; set; }
    public DMGoodsView()
    {
      InitializeComponent();
      this.DataContext = this;

      GoodsList = new ObservableCollection<GoodsModel>(GoodsModel.SelectAllGoods());
    }
    private void RadGridView_Deleting(object sender, GridViewDeletingEventArgs e)
    {
      InfoBar = string.Empty;
      foreach (GoodsModel goods in e.Items)
      {
        int ret = GoodsModel.SelectUsedGoods(goods);

        if (ret > 0)
        {
          InfoBar = $"{goods.GoodsName} 有关联的称重记录，无法删除！";
          e.Cancel = true;
          return;
        }
        if (ret < 0)
        {
          InfoBar = $"删除失败，请查看系统日志！";
          e.Cancel = true;
          return;
        }
        if (ret == 0)
        {
          ret = GoodsModel.DeleteGoods(goods);
          if (ret > 0)
          {
            InfoBar = $"物资 {goods.GoodsName} 已删除";
            log.Info(InfoBar);
          }
          else if (ret < 0)
          {
            InfoBar = $"删除失败，请查看系统日志！";
          }
          return;
        }
      }
    }

    private void RadRadioButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      GoodsModel goods = ((RadRadioButton)sender).DataContext as GoodsModel;
      GoodsModel.UpdateGoods(goods);
      if (goods.IsValid)
      {
        InfoBar = $"启用物资{goods.GoodsName}";
        log.Info(InfoBar);
      }
      else
      {
        InfoBar = $"禁用物资{goods.GoodsName}";
        log.Info(InfoBar);
      }

      GoodsList = new ObservableCollection<GoodsModel>(GoodsModel.SelectAllGoods());
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      RadButton btn = sender as RadButton;
      if (btn.Content.ToString() == "编辑" && SelectedGoods == null)
      {
        InfoBar = "请选择一条记录";
        return;
      }
      else if (btn.Content.ToString() == "新增")
      {
        SelectedGoods = null;
      }

      RadWindow window = new DMGoodsDetailView(SelectedGoods);
      window.Owner = this;

      window.ShowDialog();

      GoodsList = new ObservableCollection<GoodsModel>(GoodsModel.SelectAllGoods());
    }

    private void RadButton_Price_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      RadWindow window = new DMGoodsPriceView();
      window.Owner = this;

      window.ShowDialog();
    }
  }
}
